package com.ebelter.temperaturegun.ui.activity;


import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.views.CombinView1;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.IConstant;
import com.ebelter.temperaturegun.model.app.TemUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ModifyNameActivity extends TBaseActivity {


    public static final int ModifyName_Success = 76;

    private CombinView1 cb1_modify_name;
    private ImageView top_iv;
    private String oriName;

    @Override
    protected int setContentViewId() {
        return R.layout.activity_modify_name;
    }

    @Override
    protected void initView() {
        cb1_modify_name=findViewById(R.id.cb1_modify_name);
        top_iv=findViewById(R.id.top_iv);
        oriName=TemUtils.getUerNickName();
        cb1_modify_name.setTitle1(oriName);

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //修改
    public void doneBtClick(View view){

        final String newName= cb1_modify_name.getTitle1().trim();

        if (TextUtils.isEmpty(newName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }

        if (newName.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.equals(newName,oriName)){
            ToastUtil.show(R.string.Modify_Success);

            finish();
            return;
        }


        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, TemUtils.getUerID());
        paramMap.put(IHttpRequestField.nickName,newName);

        NetUtils.getInstance().updateInfo(this, paramMap, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }
                if (baseResponse.resultCode==0){
                    ToastUtil.show(R.string.Modify_Success);
                    UserSpUtil.writeString(IConstant.IUserSpCon.NICKNAME,newName);
                    setResult(ModifyName_Success);


                    CommonEventBus commonEventBus=new CommonEventBus(ModifyNameActivity.TAG,MainActivity.TAG+"--"+UserInfoActivity.TAG,ModifyName_Success,"昵称修改了");
                    EventBus.getDefault().post(commonEventBus);

                    finish();

                }else if (baseResponse.resultCode==112){
                    ToastUtil.show(R.string.nickname_have_been_registered);
                }else if (baseResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show(R.string.Modify_Failed);
                }

            }
        });
    }


}
